package cn.yh.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import cn.yh.dao.IAdminDao;
import cn.yh.factory.SqlSessionFactoryUtil;
import cn.yh.model.Admin;

public class AdminBiz {
	
	/**
	 * 修改管理员
	 * @param admin
	 * @return
	 */
	public int  updateAdmin(Admin admin,String npwd){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		 IAdminDao ad = session.getMapper(IAdminDao.class);
		 int aid = admin.getAid();
		 int flag = ad.updateAdmin(aid, npwd);
		 session.commit();
		 return flag;
	}
	
	
	/**
	 * 根据	ID 查询管理员
	 * @param aid
	 * @return
	 */
	public Admin searchById(int aid){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		IAdminDao ad  =session.getMapper(IAdminDao.class);
		Admin admin = ad.searchById(aid);
		return admin;
	}
	 
	/**
	 * 删除管理员
	 * @param aid
	 * @return
	 */
	public int delAdmin(int aid){
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		IAdminDao ad = session.getMapper(IAdminDao.class);
		int flag = ad.delAdmin(aid);
		session.commit();
		return flag;
	}
	
	
	
	/**
	 * 登陆管理员
	 * @param aname
	 * @param apwd
	 * @return
	 */
	public Admin adminLogin(String aname, String apwd) {
		Map<String, Object> map = new HashMap<String, Object>();
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		IAdminDao ad = session.getMapper(IAdminDao.class);
		map.put("aname", aname);
		map.put("apwd", apwd);
		Admin admin = ad.adminLogin(map);
		return admin;
	}

	/**
	 * 查看 所有管理员
	 * 
	 * @return
	 */
	public List<Admin> searchAll() {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		IAdminDao ad = session.getMapper(IAdminDao.class);
		List<Admin> list = ad.searchAll();
		return list;
	}

	/**
	 * 添加管理员
	 * 
	 * @param admin
	 * @return
	 */
	public int addAdmin(Admin admin) {
		SqlSession session = SqlSessionFactoryUtil.getSqlSession();
		IAdminDao ad = session.getMapper(IAdminDao.class);
		int flag = ad.addAdmin(admin);

		session.commit();
		return flag;
	}
}
